package Seity;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;

/**
 * This class merges all the indexes into a given directory.
 * 
 * @author Xu Sheng
 * @version 1.1.0
 */
public class MergeIndex
{
	private int FileCount;

	/**
	 * Constructor of class MergeIndex.
	 */
	public MergeIndex()
	{
		this.FileCount = 0;
	}

	/**
	 * Merge one source index to another index.
	 * 
	 * @param src
	 *            source file
	 * @param dest
	 *            destination file
	 */
	public void merge(File src, File dest)
	{
		BufferedReader input = null;
		FileWriter output = null;
		String line = null;

		try
		{
			input = new BufferedReader(new InputStreamReader(
					new FileInputStream(src)));
		}
		catch (FileNotFoundException e)
		{
			System.err.println("File " + src + " not found...");
			return;
		}

		if (dest.exists())
		{
			try
			{
				output = new FileWriter(dest, true);
				if ((line = input.readLine()) != null)
				{
					output.write(line);
				}
			}
			catch (IOException e)
			{
				System.err.println("Encounter I/O error while writing to file "
						+ dest.getAbsolutePath());
			}
			finally
			{
				try
				{
					output.close();
				}
				catch (IOException e)
				{
					System.err.println("Can't close file "
							+ dest.getAbsolutePath());
				}
			}
		}
		else
		{
			try
			{
				output = new FileWriter(dest);
				if ((line = input.readLine()) != null)
				{
					output.write(line);
				}
			}
			catch (IOException e)
			{
				System.err.println("Encounter I/O error while writing to file "
						+ dest.getAbsolutePath());
			}
			finally
			{
				try
				{
					output.close();
				}
				catch (IOException e)
				{
					System.err.println("Can't close file "
							+ dest.getAbsolutePath());
				}
			}
		}

		if (input != null)
		{
			try
			{
				input.close();
			}
			catch (IOException e)
			{
				System.err.println("Can't close file " + src.getAbsolutePath());
			}
		}
	}

	/**
	 * This method recursively merges all the indexes in a given directory.
	 * 
	 * @param src
	 *            source directory
	 * @param dest
	 *            destination directory
	 */
	public void mergeAll(String src, String dest)
	{
		LinkedList<File> list = new LinkedList<File>();
		File dir = new File(src);
		File file[] = dir.listFiles();
		for (int i = 0; i < file.length; i++)
		{
			if (file[i].isDirectory())
			{
				list.add(file[i]);
			}
			else
			{
				merge(file[i], new File(dest + "/" + file[i].getName()));
				System.out.println(++FileCount);
			}
		}
		while (!list.isEmpty())
		{
			mergeAll(list.removeFirst().getAbsolutePath(), dest);
		}
	}

	/**
	 * Entry of the program.
	 * 
	 * @param args
	 *            program arguments
	 */
	public static void main(String[] args)
	{
		MergeIndex merge = new MergeIndex();
//		merge.mergeAll("E:/TEMP/temp4", "E:/TEMP/temp5");
		merge.mergeAll("E:/Seity Search Engine/Indexing/goabroad", "E:/Seity Search Engine/Indexing/general");
	}
}
